<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hfun.server.dao${readPackageName}.R${objectName}Mapper" >
  <resultMap id="BaseResultMap" type="com.hfun.server.domain${packageName}.${objectName}Model">
    <id column="id" property="id" />
    <#list columns as item>
    <result column="${item.column}" property="${item.property}" />
    </#list>
  </resultMap>
  <!-- 可选列定义 -->
  <sql id="Base_Column_Fields" >
    ${r"${"}fields${r"}"}
  </sql>

  <!-- 查询条件sql -->
  <sql id="query_condition_sql">
    <if test="queryConditions != null and queryConditions.size()>0">
      and
      <foreach collection="queryConditions" separator=" and " item="i" open="" close="">
        <choose>
          <when test="i.type == 0">
            ${r"${"}i.columnName${r"}"}  =  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 1">
            ${r"${"}i.columnName${r"}"} <![CDATA[ < ]]>  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 2">
            ${r"${"}i.columnName${r"}"} <![CDATA[ > ]]>  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 3">
            ${r"${"}i.columnName${r"}"} <![CDATA[ <> ]]>  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 4">
            ${r"${"}i.columnName${r"}"} <![CDATA[ <= ]]>  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 5">
            ${r"${"}i.columnName${r"}"} <![CDATA[ >= ]]>  ${r"#{"}i.columnValue${r"}"}
          </when>
          <when test="i.type == 6">
            ${r"${"}i.columnName${r"}"} like CONCAT('%',${r"#{"}i.columnValue${r"}"},'%' )
          </when>
          <when test="i.type == 7">
            ${r"${"}i.columnName${r"}"} in
            <foreach collection="i.columnValue" separator="," item="l" open="(" close=")">
            ${r"#{"}l${r"}"}
            </foreach>
          </when>
          <when test="i.type == 8">
            ${r"${"}i.columnName${r"}"} not in
            <foreach collection="i.columnValue" separator="," item="l" open="(" close=")">
            ${r"#{"}l${r"}"}
            </foreach>
          </when>
          <when test="i.type == 9">
            ${r"${"}i.columnName${r"}"} like CONCAT(${r"#{"}i.columnValue${r"}"},'%' )
          </when>
          <when test="i.type == 10">
            ${r"${"}i.columnName${r"}"} IS NULL
          </when>
          <when test="i.type == 11">
            ${r"${"}i.columnName${r"}"} IS NOT NULL
          </when>
          <otherwise>
            ${r"${"}i.columnName${r"}"} = ${r"#{"}i.columnValue${r"}"}
          </otherwise>
        </choose>
      </foreach>
    </if>
  </sql>

  <!-- 排序sql -->
  <sql id="order_column_sql">
    <if test="orderColumn != null">
      <choose>
        <when test="orderDirection == null"> order by ${r"${"}orderColumn${r"}"}  desc </when>
        <when test="orderDirection == 1"> order by ${r"${"}orderColumn${r"}"}  desc </when>
        <when test="orderDirection == 2"> order by ${r"${"}orderColumn${r"}"}  asc </when>
        <when test="orderDirection == 3"> order by CONVERT(${r"${"}orderColumn${r"}"} using gbk)  desc </when>
        <when test="orderDirection == 4"> order by CONVERT(${r"${"}orderColumn${r"}"} using gbk)  asc </when>
        <otherwise> order by ${r"${"}orderColumn${r"}"}  desc </otherwise>
      </choose>
    </if>
  </sql>

  <!-- 分页处理sql -->
  <sql id="page_sql">
    <if test="size != null">
      limit
      <if test="start != null">
      ${r"#{"}start${r"}"},
      </if>
    ${r"#{"}size${r"}"}
    </if>
  </sql>

  <!-- 根据条件查询 -->
  <select id="selectByObejctPaging" parameterType="com.hfun.server.domain${packageName}.${objectName}Model" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_Fields" />
    from ${tableTop}${tableName}
    <include refid="where"/>
    <include refid="query_condition_sql"></include>
    <include refid="order_column_sql"></include>
    <include refid="page_sql"></include>
  </select>

  <sql id="where">
    where 1=1
  </sql>

  <select id="selectCountByObject" parameterType="com.hfun.domain.model${packageName}.${objectName}Model" resultType="java.lang.Integer">
    select count(id) from ${tableTop}${tableName}
    <include refid="where"/>
    <include refid="query_condition_sql"></include>
  </select>
</mapper>